package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.db.entity.CardGame;
import com.itheima.prize.commons.db.entity.CardProductDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.mapper.CardGameMapper;
import com.itheima.prize.commons.db.mapper.GameLoadMapper;
import com.itheima.prize.commons.db.mapper.ViewCardUserHitMapper;
import com.itheima.prize.commons.db.service.CardGameService;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/game")
@Api(tags = {"活动模块"})
public class GameController {
    @Autowired
    private GameLoadService loadService;
    @Autowired
    private CardGameService gameService;
    @Autowired
    private ViewCardUserHitService hitService;

    @GetMapping("/list/{status}/{curpage}/{limit}")
    @ApiOperation(value = "活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="status",value = "活动状态（-1=全部，0=未开始，1=进行中，2=已结束）",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",dataType = "int",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",dataType = "int",required = true)
    })
    public ApiResult list(@PathVariable int status,@PathVariable int curpage,@PathVariable int limit) {

        //TODO：任务4.1-活动模块-活动列表 （完成）
        Date now = new Date();//为了比较判断是不是在活动时间区间内
        QueryWrapper<CardGame> gameQueryWrapper = new QueryWrapper<>();
        switch (status) {
            case -1:
                break;//查全部
            case 0:
                gameQueryWrapper.gt("starttime",now);//未开始//gt是 “大于” 的意思
                break;
            case 1:
                gameQueryWrapper.le("starttime",now).gt("endtime",now);//进行中//le是 “小于等于” 的意思
                break;
            case 2:
                gameQueryWrapper.le("endtime",now);//已结束
                break;
        }
        gameQueryWrapper.orderByDesc("starttime");//按starttime字段倒序排”（从新到旧）
        Page<CardGame> page = gameService.page(new Page<>(curpage,limit),gameQueryWrapper);
        return new ApiResult(1,"成功",new PageBean<CardGame>(page));
    }

    @GetMapping("/info/{gameid}")
    @ApiOperation(value = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",required = true)
    })
    public ApiResult<CardGame> info(@PathVariable int gameid) {
        //TODO：任务4.2-活动模块-活动信息 （完成）
        return new ApiResult(1,"成功",gameService.getById(gameid));//gameService.getById(gameid) 是 MyBatis-Plus 框架中典型的根据 ID 查询单条数据的方法调用，通常用于获取指定 ID 的卡牌游戏详情。
    }

    @GetMapping("/products/{gameid}")
    @ApiOperation(value = "奖品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",required = true)
    })
    public ApiResult<List<CardProductDto>> products(@PathVariable int gameid) {
        //TODO：任务4.3-活动模块-奖品信息 （完成）
        return new ApiResult(1,"成功",loadService.getByGameId(gameid));//loadService.getByGameId(gameid) 是一个自定义的服务层方法调用，用于根据游戏 ID（gameid）查询相关的加载数据或资源信息。
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "中奖列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id",dataType = "int",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",dataType = "int",required = true),
            @ApiImplicitParam(name = "limit",value = "每页条数",dataType = "int",required = true)
    })
    public ApiResult list1(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit) {
        //TODO：任务4.4-活动模块-中奖列表 （完成）
        QueryWrapper<ViewCardUserHit> wrapper = new QueryWrapper<>();
        wrapper.eq("gameid",gameid);
        Page<ViewCardUserHit> page = hitService.page(new Page<>(curpage,limit),wrapper);
        return new ApiResult(1, "成功",new PageBean<ViewCardUserHit>(page));
    }
}